import { CookieUserKey } from '../common/consts'

export default async (ctx, next) => {
  const cookie = ctx.req.headers.cookie || ''
  const cookieList = cookie.split(';').map(str => {
    const [key, value] = str.trim().split('=')
    return { key, value }
  })

  const { value } = cookieList.find(entry => entry.key === CookieUserKey) || { key: CookieUserKey, value: '' }
  if (value) {
    await ctx.render('index', {})
  } else {
    await ctx.redirect('./login')
  }
}
